package com.tts.controller;

import com.tts.dto.ApiResponse;
import com.tts.entity.Book;
import com.tts.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import com.tts.annotation.OperationLog;

@RestController
@RequestMapping("/api/books")
@CrossOrigin
public class BookController {

    @Autowired
    private BookService bookService;

    @GetMapping
    public ApiResponse<Page<Book>> findAll(Pageable pageable) {
        return ApiResponse.success(bookService.findAll(pageable));
    }

    @GetMapping("/{id}")
    public ApiResponse<Book> findById(@PathVariable Long id) {
        return ApiResponse.success(bookService.findById(id));
    }

    @PostMapping
    @OperationLog(operation = "CREATE_BOOK", description = "创建新图书")
    public ApiResponse<Book> create(@RequestBody Book book) {
        return ApiResponse.success(bookService.create(book));
    }

    @PutMapping("/{id}")
    @OperationLog(operation = "UPDATE_BOOK", description = "更新图书信息")
    public ApiResponse<Book> update(@PathVariable Long id, @RequestBody Book book) {
        return ApiResponse.success(bookService.update(id, book));
    }

    @DeleteMapping("/{id}")
    @OperationLog(operation = "DELETE_BOOK", description = "删除图书")
    public ApiResponse<Void> delete(@PathVariable Long id) {
        bookService.delete(id);
        return ApiResponse.success(null);
    }
} 